package com.syni.mdd.sjb.marketing.basemarket.aop;

import com.syni.mdd.sjb.common.component.utils.common.exception.SystemErrorException;
import com.syni.mdd.sjb.common.component.utils.common.utils.AopTargetUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CpsApiUtilsAop {

    private final Logger log = LoggerFactory.getLogger(getClass());
    @Pointcut("execution(public *  com.syni.mdd.sjb.marketing.basemarket.utils.cps.CpsApiUtils.*(..))") // 两个..代表所有子目录，最后括号里的两个..代表所有参数
    public void logPointCut() {
    }

    @SuppressWarnings("unchecked")
    @Around("logPointCut()")
    public Object Interceptor(ProceedingJoinPoint pjp) throws Throwable {
        Object result;
        try {
            result = pjp.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage(),e);
            //获取方法返回值类型
            Class returnClass = AopTargetUtils.getTargetMethodReturnClass(pjp);
            if(returnClass == null){
                throw new SystemErrorException(e.getMessage());
            }
            result = returnClass.newInstance();
        }
        return result;
    }
}
